package com.jmyd.sharding.server.controller;

import com.jmyd.commons.base.dto.KeywordQueryDto;
import com.jmyd.commons.base.vo.PageBean;
import com.jmyd.commons.base.vo.Result;
import com.jmyd.commons.constants.BaseUrlConstants;
import com.jmyd.coupon.server.service.IUserService;
import com.jmyd.sharding.server.domain.dto.UserDto;
import com.jmyd.sharding.server.domain.vo.UserVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户管理相关接口
 *
 * @author samphin
 * @date 2019-8-28 16:24:10
 */
@RestController
@RequestMapping(BaseUrlConstants.BASE_ADMIN_PREFIX + "/user")
public class UserController {

    @Resource
    private IUserService userService;

    /**
     * 创建用户信息
     *
     * @param dto
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody @Validated UserDto dto) {
        boolean bl = this.userService.save(dto);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 修改用户信息
     *
     * @param dto
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody @Validated UserDto dto) {
        boolean bl = this.userService.save(dto);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 删除用户信息
     *
     * @param id 用户id
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable Long id) {
        boolean bl = this.userService.deleteById(id);
        return bl ? Result.ok() : Result.failure();
    }

    /**
     * 查询用户分页列表
     *
     * @param queryDto 查询条件
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @GetMapping
    public Result<PageBean<UserVo>> queryPageList(KeywordQueryDto queryDto) {
        PageBean<UserVo> pageBean = this.userService.queryPageList(queryDto);
        return new Result<>(pageBean);
    }

    /**
     * 查询用户详情
     *
     * @param id 用户ID
     * @author samphin
     * @since 2021-7-15 10:27:23
     */
    @GetMapping("/{id}")
    public Result<UserVo> queryById(@PathVariable Long id) {
        UserVo vo = this.userService.queryById(id);
        return new Result<>(vo);
    }
}
